package com.woniuxy.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.util.CheckTokenUtil;
import com.woniuxy.goods.email.SendEmail;
import com.woniuxy.goods.model.entity.Pull;
import com.woniuxy.goods.model.entity.PullVo;
import com.woniuxy.goods.model.vo.PageAndSize;
import com.woniuxy.goods.service.GoodsService;
import com.woniuxy.goods.service.PullService;
import com.woniuxy.model.entity.Goods;
import com.woniuxy.model.vo.GoodsVo;
import com.woniuxy.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("pull")
public class PullController {
    @Resource
    private PullService pullService;
    @Resource
    private GoodsService goodsService;

    @Autowired
    JavaMailSenderImpl mailSender;
    @PostMapping("/pullFindAll")
    public ResponseResult<IPage<PullVo>> pullFindAll(@RequestBody PageAndSize pageAndSize) {
        IPage<PullVo> list = pullService.pullFindAll(pageAndSize);
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", list);
    }
    /**
     * 修改状态为删除“1”
     */
    @GetMapping("pullDeleteById/{id}")
    public ResponseResult<Boolean> pullDeleteById(@PathVariable("id") Integer id){
        Pull pull = new Pull();
        pull.setId(id);
        pull.setStatus("1");
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",pullService.updateById(pull));
    }

    /**
     * 推送
     */
    @PostMapping("addPull")
    public ResponseResult<Boolean> pullGoods(@RequestBody List<Pull> pullList, HttpServletRequest httpServletRequest) throws MessagingException {
        String rscId = "test";
        String content="<html><body>这是有图片的邮件：<img src=\'cid:" + rscId + "\' ></body></html>";
        String u = httpServletRequest.getHeader("access_token");
        Map<String, Object> user = CheckTokenUtil.getUser(u);
        Integer aid = (Integer) user.get("uid");
        //String account = (String) user.get("account");
        for (Pull pull : pullList ) {
            pull.setFerrerId(aid);
            pull.setPullTime(new Date());
            pull.setStatus("0");
        }
        log.debug("推送信息" + pullList.toString());

        if(pullService.saveBatch(pullList)){
            for (Pull pull : pullList) {//遍历推送列表
                Goods goods = goodsService.getById(pull.getGoodsId());
                String rscPath = "D:\\" + goods.getPicture();//获取产品图片名
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
                    //内容
                    helper.setText("您好！，热卖商品"+"  "+"商品名:"+goodsService.getById(pull.getGoodsId()).getGoodsName() + content,true);
                    //邮件接收者
                    helper.setTo("1446218516@qq.com");
                    //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
                    helper.setFrom("1483854811@qq.com");

                    FileSystemResource res = new FileSystemResource(new File(rscPath));
                    helper.addInline(rscId, res);
                    mailSender.send(mimeMessage);

            }

            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        } else{
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",false);
        }
    }
}
